package tau.pimpmyfreedb.GUI;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import tau.pimpmyfreedb.common.types.MusicItem;
import tau.pimpmyfreedb.dal.DataType;
import tau.pimpmyfreedb.master.IPimpMaster;

public class AddExistWindow {

    MusicItem[] searchResults;
    DataType dataType;
    EditWindow parent;
    IPimpMaster master;
    Shell shell;
    Text tSearch;
    Label lsearch, lTitle;
    List list;
    Button bOk, bCancel, bAddNew;
    GridData gridData;
    boolean answer = false;
    int selectedIndex;

    public AddExistWindow(IPimpMaster master, EditWindow parent) {
	this.master = master;
	this.parent = parent;
	shell = SWTUtil.getDialog(parent.getShell());
	CreateShell();
    }

    // Test Only
    public AddExistWindow(EditWindow parent) {
	// this.master = master;
	this.parent = parent;
	shell = SWTUtil.getDialog(parent.getShell());
	CreateShell();
    }

    private void CreateShell() {

	shell.setLayout(new GridLayout(2, false));

	lTitle = new Label(shell, SWT.NONE);
	gridData = new GridData();
	gridData.horizontalSpan = 2;
	gridData.horizontalAlignment = SWT.FILL;
	lTitle.setLayoutData(gridData);
	lTitle.setText("Please choose an exsisting item or create a new one");

	Composite cSearch = new Composite(shell, SWT.NONE);
	cSearch.setLayout(new GridLayout(2, false));
	gridData = new GridData();
	gridData.horizontalSpan = 2;
	gridData.horizontalAlignment = SWT.FILL;
	cSearch.setLayoutData(gridData);

	new Label(cSearch, SWT.NONE).setText("Search:");
	tSearch = new Text(cSearch, SWT.BORDER);
	gridData = new GridData();
	gridData.grabExcessHorizontalSpace = true;
	gridData.horizontalAlignment = SWT.FILL;
	gridData.widthHint = 150;
	tSearch.setLayoutData(gridData);
	tSearch.addModifyListener(new ModifyListener() {
	    @Override
	    public void modifyText(ModifyEvent arg0) {
		master.searchSmall();
	    }
	});

	// list
	list = new List(shell, SWT.BORDER | SWT.V_SCROLL);
	gridData = new GridData();
	gridData.verticalSpan = 3;
	gridData.horizontalAlignment = SWT.FILL;
	gridData.grabExcessHorizontalSpace = true;
	gridData.verticalAlignment = SWT.FILL;
	gridData.heightHint = 200;
	list.setLayoutData(gridData);

	// Buttons
	bAddNew = new Button(shell, SWT.PUSH);
	bAddNew.setText("New");
	gridData = new GridData();
	gridData.horizontalAlignment = SWT.FILL;
	gridData.verticalAlignment = SWT.TOP;
	bAddNew.setLayoutData(gridData);
	bAddNew.addSelectionListener(new SelectionAdapter() {
	    @Override
	    public void widgetSelected(SelectionEvent arg0) {
		// TODO Auto-generated method stub
		super.widgetSelected(arg0);
		master.createNewItem(AddExistWindow.this, dataType);
	    }
	});

	bOk = new Button(shell, SWT.PUSH);
	bOk.setText("Select");
	gridData = new GridData();
	gridData.horizontalAlignment = SWT.FILL;
	gridData.verticalAlignment = SWT.TOP;
	bOk.setLayoutData(gridData);
	bOk.addSelectionListener(new SelectionAdapter() {
	    @Override
	    public void widgetSelected(SelectionEvent arg0) {
		// TODO Auto-generated method stub
		super.widgetSelected(arg0);
		answer = true;
		selectedIndex = list.getSelectionIndex();
		master.handleSelectedDataType(AddExistWindow.this);

	    }
	});

	bCancel = new Button(shell, SWT.PUSH);
	bCancel.setText("Cancel");
	gridData = new GridData();
	gridData.horizontalAlignment = SWT.FILL;
	gridData.verticalAlignment = SWT.TOP;
	bCancel.setLayoutData(gridData);
	bCancel.addSelectionListener(new SelectionAdapter() {
	    @Override
	    public void widgetSelected(SelectionEvent arg0) {
		// TODO Auto-generated method stub
		super.widgetSelected(arg0);
		answer = false;
		shell.close();
	    }
	});
	shell.pack();
    }

    public void setSearchResult(MusicItem[] items) {
	list.removeAll();
	list.setMenu(null);
	for (int i = 0; i < items.length; i++)
	    list.add(items[i].getName());
	searchResults = items;
    }

    public void run() {
	shell.open();
    }

    public void setMode(DataType newDataType) {
	dataType = newDataType;
	if (dataType == DataType.ALBUM) {
	    shell.setText("Add Album");
	    lTitle.setText("Please choose an exsisting album or create a new one");
	}
	if (dataType == DataType.TRACK) {
	    shell.setText("Add Track");
	    lTitle.setText("Please choose an exsisting track or create a new one");
	}
    }

    public DataType getDataType() {
	return dataType;
    }

    public MusicItem getSelection() {
	if (searchResults != null) {
	    return searchResults[selectedIndex];
	}
	return null;
    }

    public String getSearch() {
	if (tSearch != null) {
	    return tSearch.getText();
	}
	return "";
    }

    public boolean getAnswer() {
	return answer;

    }

    public Shell getShell() {
	return shell;
    }

    public EditWindow getEditWindow() {
	return parent;
    }

}
